{% extends 'manage.html' %}
{% load static %}
{% load issues %}
{% block title %}
    项目管理-问题
{% endblock %}

{% block css %}
    <link rel="stylesheet" href="{% static 'plugings/editor-md/css/editormd.min.css' %}">
    <link rel="stylesheet" href="{% static 'plugings/bootstrap-datepicker/dist/css/bootstrap-datepicker.min.css' %}">
    <link rel="stylesheet" href="{% static 'plugings/bootstrap-select/docs/docs/dist/css/bootstrap-select.min.css' %}">
    <link rel="stylesheet" href="{% static 'plugings/select2/css/select2.min.css' %}">
    <style>
        .hide {
            display: none;
        !important;
        }

        .row {
            margin: auto 20px;
        }

        .issues_list .number {
            width: 100px;
            text-align: right;
        }

        .issues_list .number a {
            font-weight: 500;
            padding: 0 10px;
        }

        .issues_list .issues .tags {
            padding: 10px 0;
        }

        .issues_list .issues .tags span {
            margin-right: 20px;
            display: inline-block;
            font-size: 12px;
        }

        .issues_list .issues .tags .type {
            color: white;
            padding: 1px 5px;
            border-radius: 5px;
        }

        .issues_name:hover {
            text-decoration: none;
        }


        /* 设置日期选择器文字为黑色 */
        .datepicker table tr td {
            color: #000 !important;
        }

        .datepicker table tr td:hover {
            color: #000 !important;
        }

        .datepicker table tr td.active {
            color: #fff !important;
        }

        .datepicker table tr th {
            color: #000 !important;
        }

        .datepicker .datepicker-switch {
            color: #000 !important;
        }

        .datepicker .prev,
        .datepicker .next {
            color: #000 !important;
        }

        .dropdown-menu .no-results {
            /* 灰色背景 */
            background-color: #f0f0f0 !important; /* 使用浅灰色，可以根据需要调整 */
            /* 黑色字体 */
            color: #000000 !important;
            /* 确保它看起来像一个列表项，而不是被禁用的文本 */
            font-weight: normal !important;
        }

        /* 针对所有 selectpicker 控件的按钮 */
        .bootstrap-select .btn {
            background-color: white !important; /* 替换为您想要的目标颜色 */
            /* 您也可以更改边框颜色 */
            border-color: #cccccc !important;
        }

        .msg {
            color: red;
        }


        nav .pagination li {
            display: inline-block; /* 水平排列 */
            margin: 10px 2px; /* 项间距 */
            list-style: none; /* 去除列表样式 */
        }

        nav .pagination li a {
            display: block; /* 让整个区域可点击 */
            padding: 8px 12px; /* 内边距 */
            text-decoration: none; /* 去除下划线 */
            color: #007bff; /* 链接颜色 */
            border: 1px solid #dee2e6; /* 边框 */
            border-radius: 4px; /* 圆角 */
            transition: all 0.2s ease; /* 过渡动画 */
            font-size: 14px;
        }

        /* 悬停状态 */
        nav .pagination li a:hover {
            background-color: #e9ecef;
            border-color: #adb5bd;
        }

        /* 当前页激活状态 */
        nav .pagination li.active a {
            background-color: #007bff;
            border-color: #007bff;
            color: white;
        }

        /* 禁用状态 */
        nav .pagination li.disabled a {
            color: #6c757d;
            pointer-events: none;
            background-color: #fff;
            border-color: #dee2e6;
            cursor: not-allowed;
        }

        /* 特殊按钮样式（上一页/下一页） */
        nav .pagination li.prev a,
        nav .pagination li.next a {
            padding: 8px 16px;
            font-weight: 500;
        }


        /* 筛选 */
        .filter-area .item {
            margin-bottom: 15px;
        }

        .filter-area .item .title {
            padding: 5px 0;
        }

        .filter-area .item .check-list a {
            text-decoration: none;
            display: inline-block;
            min-width: 65px;
        }

        .filter-area .item .check-list label {
            font-weight: 200;
            font-size: 15px;
            margin-left: 3px;
        }

        .filter-area .item .check-list a:hover {
            font-weight: 300;
        }

        .filter-area .item .check-list .cell {
            margin-right: 10px;
        }

        .input-group-btn {
            float: left;
            margin-top: 10px;
            margin-bottom: 10px;
        }

        .input-group-btn:first-child {
            margin-left: 16px;

        }

        #InviteUrl {
            float: left;
            width: 72%;
            margin-top: 10px;
            margin-bottom: 10px;
        }
        .btn-default{
            border: 1px solid grey;
        }
    </style>
{% endblock %}


{% block content %}
    <!-- Modal -->
    <div class="modal fade" id="addModalXl" tabindex="-1" aria-labelledby="addModalXlLabel">
        <div class="modal-dialog modal-xl">
            <div class="modal-content">
                <div class="modal-header">

                    <h4 class="modal-title">新建问题</h4>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body" style="padding-right: 40px">
                    <form id="addform">
                        {% csrf_token %}
                        <div class="form-group">
                            <label for="{{ form.issues_type.id_for_label }}">{{ form.issues_type.label }}</label>
                            {{ form.issues_type }}
                            <span class="msg">{{ form.issues_type.errors.0 }}</span>
                        </div>
                        <div class="form-group">
                            <label for="{{ form.subject.id_for_label }}">{{ form.subject.label }}</label>
                            {{ form.subject }}
                            <span class="msg">{{ form.subject.errors.0 }}</span>
                        </div>
                        <div class="form-group">
                            <label for="{{ form.module.id_for_label }}">{{ form.module.label }}</label>
                            {{ form.module }}
                            <span class="msg">{{ form.module.errors.0 }}</span>
                        </div>
                        <div class="form-group">
                            <label for="{{ form.desc.id_for_label }}">{{ form.desc.label }}</label>
                            <div id="editor">
                                {{ form.desc }}
                            </div>

                            <span class="msg">{{ form.desc.errors.0 }}</span>
                        </div>


                        <div class="form-group clearfix">
                            <div class="row">
                                <div class="col-md-6">
                                    <label for="{{ form.status.id_for_label }}">{{ form.status.label }}</label>
                                    <div>
                                        {{ form.status }}
                                        <span class="msg">{{ form.errors.0 }}</span>
                                    </div>
                                </div>

                                <div class="col-md-6">
                                    <label for="{{ form.priority.id_for_label }}">{{ form.priority.label }}</label>
                                    <div>
                                        {{ form.priority }}
                                        <span class="msg">{{ form.errors.0 }}</span>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="form-group clearfix">
                            <div class="row">
                                <div class="col-md-6">
                                    <label for="{{ form.assign.id_for_label }}">{{ form.assign.label }}</label>
                                    <div>
                                        {{ form.assign }}
                                        <span class="msg">{{ form.errors.0 }}</span>
                                    </div>
                                </div>

                                <div class="col-md-6">
                                    <label for="{{ form.attention.id_for_label }}">{{ form.attention.label }}</label>
                                    <div>
                                        {{ form.attention }}
                                        <span class="msg">{{ form.errors.0 }}</span>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="form-group clearfix">
                            <div class="row">
                                <div class="col-md-6">
                                    <label for="{{ form.start_date.id_for_label }}">{{ form.start_date.label }}</label>
                                    <div>
                                        {{ form.start_date }}
                                        <span class="msg">{{ form.errors.0 }}</span>
                                    </div>
                                </div>

                                <div class="col-md-6">
                                    <label for="{{ form.end_date.id_for_label }}">{{ form.end_date.label }}</label>
                                    <div>
                                        {{ form.end_date }}
                                        <span class="msg">{{ form.errors.0 }}</span>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div class="form-group clearfix">
                            <div class="row">
                                <div class="col-md-6">
                                    <label for="{{ form.mode.id_for_label }}">{{ form.mode.label }}</label>
                                    <div>
                                        {{ form.mode }}
                                        <span class="msg">{{ form.errors.0 }}</span>
                                    </div>
                                </div>

                                <div class="col-md-6">
                                    <label for="{{ form.parent.id_for_label }}">{{ form.parent.label }}</label>
                                    <div>
                                        {{ form.parent }}
                                        <span class="msg">{{ form.errors.0 }}</span>
                                    </div>
                                </div>
                            </div>
                        </div>


                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">取 消</button>
                    <button type="button" class="btn btn-primary" id="add_issues">添 加</button>
                </div>
            </div>
        </div>
    </div>
    <!-- Modal -->
    <div class="modal fade" id="inviteModal" tabindex="-1" aria-labelledby="inviteModalLabel" aria-hidden="true">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">邀请成员</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <form method="post" id="inviteForm">
                        {% csrf_token %}
                        {% for item in invite_form %}
                            <div class="form-group">
                                <label for="{{ item.id_for_label }}">{{ item.label }}</label>
                                <span style="color: #676565">{% if item.help_text %}({{ item.help_text }}
                                    ){% endif %}</span>
                                {{ item }}
                                <span class="msg"></span>
                            </div>
                        {% endfor %}
                        <button type="button" class="btn btn-primary" id="btninvitecode">生成邀请码</button>
                        <span class="msg"></span>
                    </form>

                </div>
                <div class="InviteArea hide">
                    <div class="form-group">
                        <div class="input-group-btn">
                            <input type="button" value="邀请链接" class="btn btn-default">
                        </div>
                        <input type="text" class="form-control" id="InviteUrl">
                        <div class="input-group-btn">
                            <input type="button" value="复制邀请码" class="btn btn-primary" id="CopyInvite">
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="container-fluid clearfix" style="padding: 20px 0">
        <div class="row">
            {# 筛选 #}
            <div class="col-sm-3">
                <div class="card">
                    <div class="card-header">
                        <i class="fas fa-search"></i> 筛选
                    </div>
                    <div class="card-body filter-area">
                        {# 问题类型 #}
                        <div class="item">
                            <div class="title">
                                问题类型
                            </div>
                            <div class="check-list">
                                {% for item in issues_type_filter %}
                                    {{ item }}
                                {% endfor %}
                            </div>
                        </div>
                        {# 状态#}
                        <div class="item">
                            <div class="title">
                                状态
                            </div>
                            <div class="check-list">
                                {% for item in status_filter %}
                                    {{ item }}
                                {% endfor %}
                            </div>
                        </div>
                        {# 优先级 #}
                        <div class="item">
                            <div class="title">
                                优先级
                            </div>
                            <div class="check-list">
                                {% for item in priority_filter %}
                                    {{ item }}
                                {% endfor %}
                            </div>
                        </div>
                        {# 指派者 #}
                        <div class="item">
                            <div class="title">
                                指派者
                            </div>
                            <div class="check-list">
                                {% for item in assign_filter %}
                                    {{ item }}
                                {% endfor %}
                            </div>
                        </div>
                        {# 指派者 #}
                        <div class="item">
                            <div class="title">
                                关注者
                            </div>
                            <div class="check-list">
                                {% for item in attention_filter %}
                                    {{ item }}
                                {% endfor %}
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            {# 问题 #}
            <div class="col-sm-8">
                <div class="card">
                    <div class="card-header">
                        <i class="far fa-question-circle"></i> 问题
                    </div>
                    <div class="card-body">
                        <a class="btn btn-success btn-sm" data-toggle="modal" data-target="#addModalXl">新建问题</a>
                        <a class="btn btn-primary btn-sm" data-toggle="modal" data-target="#inviteModal">邀请成员</a>
                    </div>
                    <table class="table">
                        <tbody class="issues_list">
                        {% for item in  issues_obj_list %}
                            <tr>
                                <td class="number">
                                    <i class="fa fa-circle text-{{ item.priority }}"></i>
                                    <a target="_blank"
                                       href="{% url 'manage:issues_detail' project_id=request.tracer.project.id issues_id=item.id %}">{% str_len item.id %}</a>
                                </td>
                                <td class="issues">
                                    <div>
                                        <a href="{% url 'manage:issues_detail' project_id=request.tracer.project.id issues_id=item.id %}">{{ item.subject }}</a>
                                    </div>
                                    <div class="tags">
                                            <span class="type" style="background-color: #56b8eb">
                                                {{ item.issues_type }}
                                            </span>
                                        <span>
                                                <i class="fas fa-sync"
                                                   aria-hidden="true"></i> {{ item.get_status_display }}
                                            </span>
                                        <span>
                                                <i class="far fa-user"></i> {{ item.creator }}
                                            </span>
                                        {% if item.assign %}
                                            <span>
                                                    <i class="far fa-hand-point-right"></i> {{ item.assign.username }}
                                                </span>
                                        {% endif %}
                                        {% if item.end_date %}
                                            <span>
                                                    <i class="far fa-calendar-alt"></i> {{ item.end_date }} 截止
                                                </span>
                                        {% endif %}
                                        <span>
                                                <i class="far fa-clock"
                                                   aria-hidden="true"></i> {{ item.latest_update_datetime }} 更新
                                            </span>
                                    </div>
                                </td>
                            </tr>
                        {% endfor %}
                        </tbody>
                    </table>
                </div>
                <nav aria-label="Page navigation example">
                    <ul class="pagination">
                        {{ page_html|safe }}
                    </ul>
                </nav>
            </div>
        </div>
    </div>
{% endblock %}

{% block js %}
    <script src="{% static 'plugings/editor-md/editormd.min.js' %}"></script>
    <script src="{% static 'plugings/bootstrap-datepicker/dist/js/bootstrap-datepicker.min.js' %}"></script>
    <script src="{% static 'plugings/bootstrap-datepicker/dist/locales/bootstrap-datepicker.zh-CN.min.js' %}"></script>
    <script src="{% static 'plugings/bootstrap-select/docs/docs/dist/js/bootstrap-select.min.js' %}"></script>
    <script src="{% static 'plugings/bootstrap-select/docs/docs/dist/js/i18n/defaults-zh_CN.min.js' %}"></script>
    <script src="{% static 'plugings/select2/js/select2.min.js' %}"></script>
    <script src="{% static 'plugings/select2/js/i18n/zh-CN.js' %}"></script>
    <script>
        var WIKI_UPLOAD_URL = '{% url 'manage:upload' project_id=request.tracer.project.id %}'
        var ADD_ISSUES_URL = '{% url 'manage:issues' project_id=request.tracer.project.id %}'
        var INVITE_URL = '{% url 'manage:invite_code' project_id=request.tracer.project.id %}'
        $(function () {
            // 页面框架完成时触发
            BindShownEvent();
            initDatePicker();
            AddIssues();
            initSelect2();
            BinInviteCode();
            BindCopyInviteCode();
        })
        // 复制邀请码
        function BindCopyInviteCode() {
            $('#CopyInvite').click(function () {
                var textInput = $('#InviteUrl')[0]
                textInput.select()  // 选中
                document.execCommand('Copy')  // 复制
                alert('复制成功')
            })
        }

        /*生成邀请码*/
        function BinInviteCode() {
            $('#btninvitecode').click(function () {
                $('.msg').empty()
                // 发送ajax请求\
                $.ajax({
                    url: INVITE_URL,
                    type: 'POST',
                    data: $('#inviteForm').serialize(),
                    dataType: 'JSON',
                    success: function (res) {
                        if (res.status) {
                            $('.InviteArea').removeClass('hide').find('#InviteUrl').val(res.data)
                        } else {
                            $.each(res.error, function (key, value) {
                                $('#id_' + key).next().text(value[0])
                            })
                        }
                    }
                })
            })
        }

        function initSelect2() {
            $('.select2').select2().on('select2:select', function (e) {
                // 选择某一项触发
                location.href = e.params.data.id  // e.params.data.id为后端传入的value值
            }).on('select2:unselect', function (e) {
                // 取消某一项触发
                location.href = e.params.data.id
            })
        }

        function initDatePicker() {
            $('#id_start_date, #id_end_date').datepicker({
                format: 'yyyy-mm-dd',
                startDate: '0',
                language: 'zh-CN',
                autoclose: true
            });
        }

        function BindShownEvent() {
            $('#addModalXl').on('shown.bs.modal', function (event) {
                // 对话框弹出时，内容触发
                initEditorMd();
                $('.selectpicker').selectpicker();
            })
        }

        // 初始化markdown编辑器
        function initEditorMd() {
            editormd('editor', {
                placeholder: '请输入内容',
                height: 500,
                path: "{% static 'plugings/editor-md/lib/' %}",
                imageUpload: true,
                imageFormats: ['jpg', 'jpeg', 'png', 'gif'],
                imageUploadURL: WIKI_UPLOAD_URL,
            })
        }

        function AddIssues() {
            $("#add_issues").click(function () {
                    $('#addform').find('.msg').empty()
                    $.post(ADD_ISSUES_URL, $('#addform').serialize(),
                        function (res) {
                            if (res.status) {
                                console.log('成功')
                                location.reload()
                            } else {
                                // 错误信息
                                $.each(res.error, function (key, value) {
                                    if (key == 'desc' || key == 'assign' || key == 'attention') {
                                        $('#id_' + key).parent().next('.msg').text(value[0])
                                    } else {
                                        $('#id_' + key).next('.msg').text(value[0])
                                    }
                                })
                            }
                        })
                }
            )
        }


    </script>
{% endblock %}